package com.spc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spc.constant.RedisConstant;
import com.spc.dao.SetMealDao;
import com.spc.entity.PageResult;
import com.spc.entity.QueryPageBean;
import com.spc.pojo.Setmeal;
import com.spc.service.SetMealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    //从文件中读取要生成的html对应目录
    @Value("${out_put_path}")
    private String outPutPath;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //存储setMeal到数据库
        setMealDao.add(setmeal);
        //获取自动产生的id
        Integer setmealId = setmeal.getId();
        //存储关系列表
        this.saveId(checkgroupIds,setmealId);
        //将图片名称存入redis
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);

        //当添加完成后重新生成静态页面
        generateMobileStaticHtml();

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //取出分页条件
        Integer currentPage = queryPageBean.getCurrentPage();//当前页码
        Integer pageSize = queryPageBean.getPageSize();//每页记录数
        String queryString = queryPageBean.getQueryString();//查询条件
        //调用dao完成分页查询，使用mybatis分页助手插件
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setMealDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Setmeal> rows = page.getResult();
        PageResult pageResult = new PageResult(total, rows);

        return pageResult;
    }

    //查询所有数据
    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> list = setMealDao.findAll();
        return list;
    }

    @Override
    public Setmeal findById(Integer id) {
        //查询单个setmeal
        Setmeal setmeal = setMealDao.findById(id);
        return setmeal;
    }

    //套餐预约占比数据
    @Override
    public List<Map<String,Object>> findSetmealCount() {
        List<Map<String,Object>> setmealCount = setMealDao.findSetmealCount();
        return setmealCount;
    }

    //建立检查组检查项关联关系
    public void saveId(Integer[] checkgoupIds,int setmealId){
        if (checkgoupIds != null && checkgoupIds.length > 0){
            Map<String,Object> map = null;
            for (Integer checkgoupId : checkgoupIds) {
                map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgoupId);
                setMealDao.saveId(map);
            }
        }

    }

    //生成静态页面
    public void generateHtml(String templateName,String htmlPageName,Map map){
        //获得配置对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer out = null;
        try {
            Template template = configuration.getTemplate(templateName);
            //out = new FileWriter(new File(outPutPath+"/"+htmlPageName));
            out =  new OutputStreamWriter(new FileOutputStream(outPutPath+"/"+htmlPageName),"utf-8");
            //输出文件
            template.process(map,out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //生成移动端静态页面
    public void generateMobileStaticHtml(){
        //在生成静态页面之前查询数据
        List<Setmeal> list = setMealDao.findAll();

        //需要生成套餐列表页面
        generateMobileSetMealStaticHtml(list);

        //需要生成套餐详情页面
        generateMobileSetMealDetailHtml(list);

    }

    //生成套餐列表页面
    public void generateMobileSetMealStaticHtml(List<Setmeal> list){
        //提供数据
        Map map = new HashMap();
        map.put("setmealList",list);
        generateHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }

    //需要生成套餐详情页面
    public void generateMobileSetMealDetailHtml(List<Setmeal> list){
        for (Setmeal setmeal : list) {
            Map map = new HashMap();
            map.put("setmeal",setMealDao.findById(setmeal.getId()));
            generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }
    }
}
